package com.aftersale.platform.controller;

import com.aftersale.platform.dto.ApiResponse;
import com.aftersale.platform.entity.Ticket;
import com.aftersale.platform.entity.User;
import com.aftersale.platform.service.TicketService;
import com.aftersale.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/tickets")
public class TicketController {

    @Autowired
    private TicketService ticketService;

    @Autowired
    private UserService userService;

    // 用户提交工单
    @PostMapping
    public ResponseEntity<ApiResponse<Ticket>> submitTicket(@RequestBody Ticket ticket) {
        // 从ticket获取用户名，查找用户ID并设置到ticket对象
        User user = userService.findByUsername(ticket.getUsername());
        if (user == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error("用户不存在！"));
        }
        ticket.setUserId(user.getId());
        ticketService.save(ticket);
        return ResponseEntity.ok(ApiResponse.success(ticket, "售后单提交成功！"));
    }

    // 获取所有工单 (管理员)
    @GetMapping("/admin")
    public ResponseEntity<ApiResponse<List<Ticket>>> getAllTickets() {
        List<Ticket> tickets = ticketService.findAll();
        return ResponseEntity.ok(ApiResponse.success(tickets, "获取所有工单成功"));
    }

    // 获取某个用户的工单
    @GetMapping("/user/{username}")
    public ResponseEntity<ApiResponse<List<Ticket>>> getUserTickets(@PathVariable String username) {
        User user = userService.findByUsername(username);
        if (user == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error("用户不存在！"));
        }
        List<Ticket> tickets = ticketService.findByUserId(user.getId());
        return ResponseEntity.ok(ApiResponse.success(tickets, "获取用户工单成功"));
    }

    // 更新工单状态 (管理员)
    @PutMapping("/{id}/status")
    public ResponseEntity<ApiResponse<Ticket>> updateTicketStatus(@PathVariable Integer id,
                                                                  @RequestBody Map<String, String> payload) {
        String newStatus = payload.get("newStatus");
        String newProgressStep = payload.get("newProgressStep");

        Ticket existingTicket = ticketService.findById(id);
        if (existingTicket == null) {
            return ResponseEntity.notFound().build();
        }

        existingTicket.setStatus(Ticket.TicketStatus.valueOf(newStatus)); // 更新状态
        // 更新进度，这里简单地添加新步骤
        if (newProgressStep != null && !newProgressStep.trim().isEmpty()) {
            List<String> progressList = existingTicket.getProgressList();
            if (!progressList.contains(newProgressStep)) {
                progressList.add(newProgressStep);
                existingTicket.setProgressList(progressList); // 更新内部List
            }
        }

        ticketService.update(existingTicket);
        return ResponseEntity.ok(ApiResponse.success(existingTicket, "工单状态更新成功！"));
    }

    // TODO: 其他工单管理操作，如分配、备注等
}